package io.gitee.yxsnake.framework.core.utils;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import io.gitee.yxsnake.framework.core.exception.ServiceException;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.util.Collection;
import java.util.Map;
import java.util.Objects;

/**
 * @author snake
 * @description
 * @since 2024/11/13 12:59
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class BizAssert {

    public static void equals(String msg,Integer code, Object obj1, Object obj2, Object... params) {
        if (Objects.equals(obj1, obj2)) {
            failure(msg,code, params);
        }
    }

    public static void isTrue(String msg,Integer code,boolean condition, Object... params) {
        if (condition) {
            failure(msg,code, params);
        }
    }

    public static void isFalse(String msg,Integer code,boolean condition, Object... params) {
        if (!condition) {
            failure(msg,code, params);
        }
    }

    public static void isNull(String msg,Integer code, Object condition, Object... params) {
        if (Objects.isNull(condition)) {
            failure(msg,code, params);
        }
    }

    public static void nonNull(String msg,Integer code, Object condition, Object... params) {
        if (Objects.nonNull(condition)) {
            failure(msg,code, params);
        }
    }

    public static void equals(String message, Object obj1, Object obj2, Object... params) {
        if (Objects.equals(obj1, obj2)) {
            failure(message, params);
        }
    }

    public static void isTrue(String message, boolean condition, Object... params) {
        if (condition) {
            failure(message, params);
        }
    }

    public static void isFalse(String message, boolean condition, Object... params) {
        if (!condition) {
            failure(message, params);
        }
    }

    public static void isNull(String message, Object condition, Object... params) {
        if (Objects.isNull(condition)) {
            failure(message, params);
        }
    }

    public static void nonNull(String message, Object condition, Object... params) {
        if (Objects.nonNull(condition)) {
            failure(message, params);
        }
    }

    public static void failure(String msg,Integer code) {
        throw new ServiceException(msg,code);
    }

    public static void failure(String message, Object... params) {
        throw new ServiceException(String.format(message,params));
    }

    public static void notEmpty(String msg,Integer code, Object[] array, Object... params) {
        if (ObjectUtil.isEmpty(array)) {
            failure(msg, code,params);
        }
    }

    public static void noNullElements(String msg,Integer code, Object[] array) {
        if (array != null) {
            for (Object element : array) {
                if (element == null) {
                    failure(msg,code);
                }
            }
        }
    }

    public static void notEmpty(String msg,Integer code, Collection<?> collection, Object... params) {
        if (CollUtil.isEmpty(collection)) {
            failure(msg,code, params);
        }
    }

    public static void notEmpty(String msg,Integer code, Map<?, ?> map, Object... params) {
        if (MapUtil.isNotEmpty(map)) {
            failure(msg, code,params);
        }
    }

    public static void isEmpty(String msg,Integer code, Collection<?> collection, Object... params) {
        if (CollUtil.isNotEmpty(collection)) {
            failure(msg,code, params);
        }
    }

    public static void isEmpty(String msg,Integer code, Map<?, ?> map, Object... params) {
        if (MapUtil.isNotEmpty(map)) {
            failure(msg,code, params);
        }
    }

    public static void notEmpty(String message, Object[] array, Object... params) {
        if (ObjectUtil.isEmpty(array)) {
            failure(message, params);
        }
    }

    public static void noNullElements(String message, Object[] array) {
        if (array != null) {
            for (Object element : array) {
                if (element == null) {
                    failure(message);
                }
            }
        }
    }

    public static void notEmpty(String message, Collection<?> collection, Object... params) {
        if (CollUtil.isNotEmpty(collection)) {
            failure(message, params);
        }
    }

    public static void notEmpty(String message, Map<?, ?> map, Object... params) {
        if (MapUtil.isNotEmpty(map)) {
            failure(message, params);
        }
    }

    public static void isEmpty(String message, Collection<?> collection, Object... params) {
        if (CollUtil.isEmpty(collection)) {
            failure(message, params);
        }
    }

    public static void isEmpty(String message, Map<?, ?> map, Object... params) {
        if (MapUtil.isEmpty(map)) {
            failure(message, params);
        }
    }
}

